package com.hb.ssmdemo.tools;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

/**
 * 
 * @ClassName:JSONConvertUtils
 * @Description:json转换
 */
public class JSONConvertUtils {
	private static final Logger log = Logger.getLogger(JSONConvertUtils.class);

	/**
	 * 用于转换查询list成为json
	 * @param list
	 * @return
	 */
	public static String convertList2String(List list) {
		if (list == null)
			return "";
		JSONArray array = new JSONArray();
		for (Iterator ite = list.iterator(); ite.hasNext();) {

			Object o = ite.next();
			JSONObject object = (JSONObject) getJsonFromModel(o);
			array.add(object);
		}
		return array.toString();
	}

	public static String convertObject2String(Object o) {
		if (o == null)
			return "";
		JSONObject object = (JSONObject) getJsonFromModel(o);
		return object.toString();
	}

	public static String convertMap2String(Map map) {
		if (map == null)
			return "";
		JSONObject object = JSONObject.fromObject(map);
		return object.toString();
	}

	public static String convertSingleList2String(List list) {
		if (list == null)
			return "";
		JSONArray array = JSONArray.fromObject(list);
		return array.toString();
	}

	public static void main(String args[]) {
	}

	/**
	 * 包含多对,的对象.但不包括一对多的集合对
	 * 
	 * @return
	 */
	private static Object getJsonFromModel(Object model) {
		JSONObject jsonObject = new JSONObject();
		Method[] methods = model.getClass().getMethods();
		for (int i = 0; i < methods.length; i++) {
			Method method = methods[i];
			/* BaseModel中不要getClass */
			if (method.getName().equals("getClass")) {
				continue;
			}
			if (!ModelUtils.isGetMethod(method)) {
				continue;
			}
			Class returnType = method.getReturnType();
			/* 不需要转换的类型 */
			if (returnType == byte[].class || returnType == Logger.class) {
				continue;
			}
			String propertyName = ModelUtils.getPropertyName(method.getName());
			Object getObj = ModelUtils.invokeGetMethod(model, method);
			if (getObj == null) {
				continue;
			}
			if ((getObj instanceof Calendar)) {
				getObj = DateUtils.calendar2String((Calendar) getObj);
			} else if (getObj instanceof Date) {
				getObj = DateUtils.date2String((Date) getObj);
			} else if (getObj instanceof Collection) {
				continue;
			} else if ((getObj instanceof Integer)
					|| (getObj instanceof String)
					|| (getObj instanceof Boolean) || (getObj instanceof Byte)
					|| (getObj instanceof Long) || (getObj instanceof Short)
					|| (getObj instanceof BigDecimal)
					|| (getObj instanceof Double) || (getObj instanceof Float)) {

			} else {
				getObj = getJsonFromModel(getObj);
			}

			try {
				jsonObject.put(propertyName, getObj);
			} catch (Exception ex) {
				log.info("转换错误：propertyName[" + propertyName + "],ex:"
						+ ex.getMessage());
				ex.printStackTrace();
			}

		}
		return jsonObject;
	}

}
